package cse.om.common;

import cse.om.vswn.Sensitivity;
import edu.stanford.nlp.ling.WordTag;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

public class Converter {
	
	static final SimpleDateFormat sdf;
	
	static {
		sdf = new SimpleDateFormat("YYYY/MM/dd HH:mm:ss");
//		sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
	}
	
	public static Date unixTimestamp2Date(String timestamp) {
		long t = Long.parseLong(timestamp);
		t = (t) * 1000;
		return new Date(t);
	}
	
	public static String unixTimestamp2DateSimpleFormat(String timestamp) {
		return date2SimpleFormat(unixTimestamp2Date(timestamp));
	}
	
	public static String date2SimpleFormat(Date date) {
		return sdf.format(date);
	}

	public static String listToMultilineString(List<String> normalizedStrings) {
		StringBuilder sb = new StringBuilder();
		for (String s : normalizedStrings) {
			sb.append(s).append('\n');
		}
		return sb.toString();
	}
	
	public static String listSentiToMultilineString(List<Sensitivity> sensitivities) {
		StringBuilder sb = new StringBuilder();
		for (Sensitivity senti : sensitivities) {
			String[] w = senti.getWord().split("#");
			sb.append(w[0]).append(": ").append(senti.getDescription())
				.append('\n');
		}
		return sb.toString();
	}

	public static String listToHtmlString(List<List<WordTag>> taggedWordTags) {
		StringBuilder sb = new StringBuilder();
		sb.append("<p style=\"margin-top: 0 font-family: monospaced\">");
		
		for (List<WordTag> lwt : taggedWordTags) {
			StringBuilder s = new StringBuilder();
			String color;
			for (WordTag wt : lwt) {
				if (wt.tag().compareTo("N") == 0 || wt.tag().compareTo("Np") == 0) {
					color = "blue";
				} else if (wt.tag().compareTo("V") == 0) {
					color = "red";
				} else if (wt.tag().compareTo("A") == 0) {
					color = "green";
				} else {
					color = "magenta";
				}
				s = s.append(wt.word().replaceAll("<", "&lt;").replaceAll(">", "&gt;"));
				s = s.append("<font color=").append(color);
				s = s.append(">").append('/').append(wt.tag()).append("</font> ");
			}
			sb.append(s.toString()).append("<br>");
		}
		
		sb.append("</p>");
		return sb.toString();
	}
}
